import * as WebSocket from 'ws';
import {Context} from "./context";

export class IO {
  private msgMap = new Map();

  on(type: string, cb: Function) {
    this.msgMap.set(type, cb);
  }

  onmessage(message: Message<any>, ctx: Context) {
    const cb = this.msgMap.get(message.$router);
    cb(message.data, ctx);
  }
}

interface Message<T> {
  $router: string
  data: T
}

export const io = new IO();